
package edu.hm.devcamp.presentation.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import edu.hm.devcamp.presentation.activities.GeoAR;

public class ARSurface extends SurfaceView {

    private Camera mCamera;

    private SurfaceHolder mSurfaceHolder;

    private static final String TAG = "ARSurface";

    public ARSurface(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ARSurface(Context context) {
        super(context);
        init();
    }

    private void init() {
        mSurfaceHolder = this.getHolder();
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mSurfaceHolder.addCallback(mSurfaceHolderListener);
    }

    SurfaceHolder.Callback mSurfaceHolderListener = new SurfaceHolder.Callback() {
        public void surfaceCreated(SurfaceHolder holder) {
            mCamera = Camera.open();

            try {
                mCamera.setPreviewDisplay(mSurfaceHolder);
            } catch (Exception e) {
                // TODO: specific catching
            }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            Parameters params = mCamera.getParameters();
            params.setPreviewSize(width, height);
            params.setPictureFormat(PixelFormat.JPEG);
            mCamera.setParameters(params);
            mCamera.startPreview();

            int currentapiVersion = android.os.Build.VERSION.SDK_INT;
            if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO) {
                ARPoiView.setCameraAngle(mCamera.getParameters().getHorizontalViewAngle());
            } else {
                ARPoiView.setCameraAngle(54);
            }
        }

        public void surfaceDestroyed(SurfaceHolder arg0) {
            mCamera.stopPreview();
            mCamera.release();
        }
    };
}
